package com.backstage.controller.login;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.backstage.controller.BaseController;
import com.backstage.entity.AjaxResult;
import com.backstage.entity.po.SysUser;
import com.exception.LoginException;

@Controller
@RequestMapping(value="/blog")
public class LoginAndLoginoutController extends BaseController{

	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String getLoginMethod(HttpServletRequest request){
		return "index";
	}
	
	
	@ResponseBody
	@RequestMapping(value="/loginForm",method=RequestMethod.POST)
	public ModelAndView loginForm(String viewName,ModelAndView modelAndView){
		modelAndView.setViewName(viewName);
		return modelAndView;
	}
	
	@ResponseBody
	@RequestMapping(value="/postLogin")
	public AjaxResult login(HttpServletRequest request,@ModelAttribute("sysUser") SysUser sysUser) throws Exception{
		/*Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(sysUser.getUsercode(), sysUser.getPassword());*/
		String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
		if(exceptionClassName!=null){
		System.out.print(exceptionClassName);
		if(UnknownAccountException.class.getName().equals(exceptionClassName)){
			return fail("账号不存在");
		}else if(LockedAccountException.class.getName().equals(exceptionClassName)){
			return fail("账号被锁");
		}else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)){
			return fail("用户名或密码错误");
		}
		return fail("未知错误");
		}else{
			Session session = SecurityUtils.getSubject().getSession();
			SysUser currentUser =  (SysUser) session.getAttribute("currentUser");
			if(currentUser == null){
				throw new LoginException("没有登录");
			}
			return success(loginSuccess);
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/welcome",method=RequestMethod.GET)
	public AjaxResult welcome(HttpServletRequest request){
	      return success(loginSuccess);
	}
	
	@RequestMapping(value="/home",method=RequestMethod.GET)
	public String home(Model model){
		Subject subject =  SecurityUtils.getSubject();
		SysUser sysUser = (SysUser)subject.getPrincipal();
		model.addAttribute("sysUser", sysUser);
		return "system/main";
	}
	
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		session.invalidate();
		return "redirect:/blog/login";
	}
}
